package com.project.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.common.Result;
import com.project.server.model.DishCategory;
import com.project.server.service.DishCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "菜品分类管理")
@RestController
@RequestMapping("/api/category")
@RequiredArgsConstructor
public class DishCategoryController {

    @Autowired
    private DishCategoryService categoryService;

    @Operation(summary = "分页查询菜品分类列表")
    @GetMapping("/page")
    public Result<Page<DishCategory>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Long restaurantId,
            @RequestParam(required = false) Integer status) {
        return Result.success(categoryService.page(pageNum, pageSize, name, categoryId, restaurantId, status));
    }

    @Operation(summary = "获取菜品分类详情")
    @GetMapping("/{id}")
    public Result<DishCategory> getById(@PathVariable Long id) {
        return Result.success(categoryService.getById(id));
    }

    @Operation(summary = "新增菜品")
    @PostMapping
    public Result<Void> save(@RequestBody @Validated DishCategory dishCategory) {
        categoryService.save(dishCategory);
        return Result.success();
    }

    @Operation(summary = "更新菜品")
    @PutMapping
    public Result<Void> update(@RequestBody @Validated DishCategory dishCategory) {
        categoryService.update(dishCategory);
        return Result.success();
    }

    @Operation(summary = "删除菜品")
    @DeleteMapping("/{id}")
    public Result<Void> remove(@PathVariable Long id) {
        categoryService.removeById(id);
        return Result.success();
    }


    @Operation(summary = "分页查询菜品分类列表")
    @GetMapping("/getList")
    public Result<List<DishCategory>> getList(@RequestParam(required = false) Long restaurantId) {
        return Result.success(categoryService.getList(restaurantId));
    }




}
